#include "sql.h"
#include "settings.h"
#include <QtSql/QtSql>
#include <QDebug>
#include <unistd.h>

#define DB1 "/var/www/db/sensor.db"

SQL::SQL(Settings *setting)
    : mSetting(setting)
{
    openDB();
    getWriteCommand();
}

SQL::~SQL() {
    closeDB();
}

bool SQL::openDB() {
    if (mDb.isOpen())
        return 1;
    else if (!mDb.isOpen()) {
        mDb = QSqlDatabase::addDatabase("QSQLITE");

        const char *dbName = NULL;
        if (QFile::exists(DB1))
            dbName = DB1;

        if (!dbName) {
            qDebug() << "Es wurde keine Datenbank gefunden, bitte überprüfen Sie die Angaben.";
            return 0;
        }

        mDb.setDatabaseName(dbName);

        return mDb.open();
    }
    else {
        return 0;
    }
}

void SQL::closeDB() {
    mDb.close();
}


void SQL::insertValue(float wert, QString entity) {
    QString queryValueString;
    QSqlQuery query;

    int entityID, sensorID;

    entityID = getEntityID(entity);
    sensorID = getSensorID();

    queryValueString = "insert into 'values'(value, fEntityID, date, fSensorID) values(%1, %2, datetime(), %3);";
    query.exec(queryValueString.arg(wert).arg(entityID).arg(sensorID));
}

void SQL::getWriteCommand() {
    QString queryString, entity = "Temperatur";
    QSqlQuery query;

    int sensorID, entityID;

    sensorID = getSensorID();

    for (int i = 0; i < 2; i++) {
        entityID = getEntityID(entity);

        queryString = "select writeCmd from MeasureOptions where fSensorID=%1 and fEntityID=%2;";
        query.exec(queryString.arg(sensorID).arg(entityID));

        query.next();

        if (i == 0) {
            mTempCommand = query.value(0).toString();
            entity = "Feuchtigkeit";
        }
        else if (i == 1) {
            mHumiCommand = query.value(0).toString();
            entity = "Temperatur";
        }
    }
}

QString SQL::getTempCommand() {
    return mTempCommand;
}

QString SQL::getHumiCommand() {
    return mHumiCommand;
}

int SQL::getEntityID(QString entity) {
    QString queryString;
    QSqlQuery query;

    int entityID;

    queryString = "select EntityID from entity where name='%1';";
    query.exec(queryString.arg(entity));

    query.next();
    entityID = query.value(0).toInt();

    return entityID;
}

int SQL::getSensorID() {
    QString queryString, sensor;
    QSqlQuery query;

    int sensorID;

    sensor = mSetting->getSensors();

// SensorID aus der DB holen
    queryString = "select SensorID from sensors where name='%1';";
    query.exec(queryString.arg(sensor));


    query.next();
    sensorID = query.value(0).toInt();

    return sensorID;
}

QString SQL::getAddress() {
    QString sensor;
    QString queryString;
    QSqlQuery query;

    sensor = mSetting->getSensors();

    queryString = "select address from sensors where name='%1';";

    query.exec(queryString.arg(sensor));

    query.next();

    return query.value(0).toString();
}

QString SQL::getDriver() {
    QString driver, queryString, sensor;
    QSqlQuery query;

    sensor = mSetting->getSensors();

    queryString = "select driver from sensors where name ='%1';";

    query.exec(queryString.arg(sensor));
    query.next();

    driver = query.value(0).toString();

    return driver;
}
